package com.zdkj.subway.check.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.check.entity.SwAuConductorShort;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuConductorShortMapper;
import com.zdkj.subway.check.mapper.SysUserMapper;
import com.zdkj.subway.check.result.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;
/**
 * 线网层级确认-售票员票款差异确认-售票员补款确认通知
 */
@Service
public class SwAuConductorShortService {

    @Resource
    private SwAuConductorShortMapper swAuConductorShortMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 售票员补款确认通知 列表
     * @param jsonObject
     * @return
     */
    public Result getSwAuConductorNoticeList(JSONObject jsonObject) {
        //售票员id
        Integer conductorPersonId = jsonObject.getInteger("conductorPersonId");
        String conductorPersonCode = jsonObject.getString("conductorPersonCode");//售票员工号
        //线路站点
        String lineCode = jsonObject.getString("lineCode");
        String stationCode = jsonObject.getString("stationCode");
        //收入时间
        Date startTime = jsonObject.getDate("startTime");
        Date endTime = jsonObject.getDate("endTime");
        Date checkTime = jsonObject.getDate("checkTime");
        Date supPaymentTime = jsonObject.getDate("supPaymentTime");
        //售票员确认状态
        //0：未确认
        //1：已确认
        String confirmStatus = jsonObject.getString("confirmStatus");
        //是否补款
        //1:补款
        //0:未补款
        String isSupPayment = jsonObject.getString("isSupPayment");
        String isNotice = jsonObject.getString("isNotice");
        //分页
        Integer startPage = jsonObject.getInteger("currentPage");
        Integer pageSize = jsonObject.getInteger("pageSize");

        Example example = new Example(SwAuConductorShort.class);
        example.setOrderByClause("CHECK_TIME desc");
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(lineCode)) {
            criteria.andEqualTo("lineCode", lineCode);
        }
        if (!StringUtils.isEmpty(stationCode)) {
            criteria.andEqualTo("stationCode", stationCode);
        }
        if(StringUtils.isEmpty(lineCode)) {
            PigxUser user = SecurityUtils.getUser();
            Map<String, Object> p1 = new HashMap<>();
            p1.put("user_code", user.getUserCode());
            List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
            SysUser sysUser = sysUsers.get(0);
            String managementLineStation = sysUser.getManagementLineStation();
            //管辖线路
            if (!StringUtils.isEmpty(managementLineStation)) {
                JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
                List<String> LineObjects = new ArrayList<>();
                for (int i = 0; i < managementLineStations.size(); i++) {
                    JSONObject jsonObject1 = managementLineStations.getJSONObject(i);
                    lineCode = jsonObject1.getString("line");
                    LineObjects.add(lineCode);
                }
                criteria.andIn("lineCode", LineObjects);
            }else {
                //不管辖线路
                criteria.andEqualTo("lineCode", sysUser.getLineCode());
            }
        }
        if (conductorPersonId != null) {
            criteria.andEqualTo("conductorPersonId", conductorPersonId);
        }
        if (!StringUtils.isEmpty(conductorPersonCode)) {
            criteria.andLike("conductorPersonCode", conductorPersonCode);
        }
        if (!StringUtils.isEmpty(confirmStatus)) {
            criteria.andEqualTo("confirmStatus", confirmStatus);
        }
        if (!StringUtils.isEmpty(isSupPayment)) {
            criteria.andEqualTo("isSupPayment", isSupPayment);
        }

        if (startTime != null) {
            criteria.andGreaterThan("incomeSettleBeginTime",startTime);
        }
        if (endTime != null) {
            criteria.andLessThan("incomeSettleEndTime",endTime);
        }
        if (checkTime != null) {
            criteria.andEqualTo("checkTime",checkTime);
        }
        if (supPaymentTime != null) {
            criteria.andEqualTo("supPaymentTime",supPaymentTime);
        }
        criteria.andEqualTo("isNotice", "1");
        PageHelper.startPage(startPage,pageSize);
        List<SwAuConductorShort> swAuConductorShorts = swAuConductorShortMapper.selectByExample(example);
        PageInfo<SwAuConductorShort> swAuConductorShortPageInfo = new PageInfo<>(swAuConductorShorts);
        JSONObject resultJson = new JSONObject();
        resultJson.put("list",swAuConductorShorts);
        resultJson.put("pageInfo",swAuConductorShortPageInfo);
        return new Result(ResultDetailMsg.OK,resultJson);
    }

    /**
     * 售票员补款确认通知 单独查询
     * @param jsonObject
     * @return
     */
    public Result getSwAuConductorNotice(JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        SwAuConductorShort swAuConductorShort = swAuConductorShortMapper.selectByPrimaryKey(id);
        JSONObject resultJson = new JSONObject();
        resultJson.put("obj",swAuConductorShort);
        return new Result(ResultDetailMsg.OK,resultJson);
    }

    public Result updateSwAuConductorNotice(JSONObject jsonObject) {
        SwAuConductorShort swAuConductorNotice =
                JSONObject.parseObject(jsonObject.toJSONString(), SwAuConductorShort.class);
        int i = swAuConductorShortMapper.updateById(swAuConductorNotice);
        return new Result(ResultDetailMsg.OK);
    }
}
